%               description
% function generates an array of numbers by the formula modified cusp-like filter:
% D_k(n) = exp(n) - exp(n-k)
% D_1(n) = exp(n) - exp(n-1)
% P(n) = P(n-1) + D_k(n) - k * d_1(n-l), {n>=0}
% Q(n) = Q(n-1) + M2 * P(n), {n>=0}
% Res(n) = Res(n-1) + Q(n) + M1 * P(n), {n>=0}
% 
% input data:
% exp - array of exponent
% L_const, K_const, M1_const, M2_const - constant of filter

function [ Results ] = V6_filter( exp, L_const, K_const, M1_const, M2_const )
    Size_of_exp = length(exp);
    
    D_k_arr = 1:Size_of_exp; %initial array #1
    D_1_arr = 1:Size_of_exp; %initial array #2
    P_array = 1:Size_of_exp; %intermediate array #1
    Q_array = 1:Size_of_exp; %intermediate array #2
    Results = 1:Size_of_exp;
    
    for i = 1:Size_of_exp
        D_k_arr(i) = exp(i);
        if (i - K_const >= 1)
           D_k_arr(i) = D_k_arr(i) - exp(i - K_const);
        end
        
        D_1_arr(i) = exp(i);
        if (i - 1 >= 1)
           D_1_arr(i) = D_1_arr(i) - exp(i - 1);
        end
    end
    
    % Setting of initial data for the recurrent formulas
    P_array(1) = D_k_arr(1);
	Q_array(1) = M2_const * P_array(1);
    Results(1) = Q_array(1) + M1_const * P_array(1);
    
    % Calculation of by the recurrent formulas
    for i = 2:Size_of_exp
        P_array(i) = P_array(i-1) + D_k_arr(i);
        if (i - L_const >= 1)
            P_array(i) = P_array(i) - K_const * D_1_arr(i - L_const);
        end
        Q_array(i) = Q_array(i-1) + M2_const * P_array(i);
        Results(i) = Results(i-1) + Q_array(i) + M1_const * P_array(i);
    end 
    
    % Find maximum of array: results, exp and normalize results
    max_of_exp = max(exp);
    max_of_results = max(Results);
    for i=1:Size_of_exp
        Results(i) = max_of_exp * Results(i) / max_of_results;
    end
end

